package com.jy.adscm.hospital.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.annotation.AdscmLogger;
import com.jy.adscm.bean.system.JyCode;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.declarationForm.bean.JyDeclarationForm;
import com.jy.adscm.declarationForm.service.JyDeclarationFormService;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.hospital.bean.JyHospitalBase;
import com.jy.adscm.hospital.service.JyHospitalBaseService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 医院基础信息表 前端控制器
 * </p>
 *
 * @author yyy
 * @since 2019-01-16
 */
@RestController
@RequestMapping("/adscm/dealer/jyHospitalBase")
public class JyHospitalBaseController {


    @Autowired
    JyHospitalBaseService jyHospitalBaseService;

    @Autowired
    JyDeclarationFormService jyDeclarationFormService;
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;

    /**
     * 查询所有医院基础信息集合
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("/queryAllHospitalBaseList")
    @AdscmLogger("查询所有医院基础信息集合")
    public SimpleCoreResponse queryAllHospitalBaseList() throws AdscmSystemException {

        return new SimpleCoreResponse("200","查询成功",jyHospitalBaseService.queryAllHospitalBaseList());
    }
    /**
     * 查询所有医院列表，只有名称和ID 的
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("/queryAllHospitalList")
    @AdscmLogger("查询所有医院信息，只有名称和ID")
    public SimpleCoreResponse queryAllHospitalList() throws AdscmSystemException {

        return new SimpleCoreResponse("200","查询成功",jyHospitalBaseService.queryAllHospitalList());
    }

    /**
     * 查询某个医院的基础信息详情
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("/queryHospitalBase/{id}")
    @AdscmLogger("查询某个医院的基础信息详情")
    public SimpleCoreResponse queryHospitalBaseById(@PathVariable Long id) throws AdscmSystemException {

        return new SimpleCoreResponse("200","查询成功",jyHospitalBaseService.queryHospitalBaseById(id));
    }
    /**
     * 增加医院的基础信息
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("/addHospitalBase")
    @AdscmLogger("增加医院的基础信息")
    public SimpleCoreResponse addHospitalBase(@RequestBody JyHospitalBase hospitalBase, HttpServletRequest request) throws AdscmSystemException {

        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);

        if (token != null){
            JyUser user = token.getUser();
            if (user != null){
                hospitalBase.setInputUserId(user.getId());
                if (user.getUnitId() == 0){
                    //表示是生产厂商
                    hospitalBase.setInputUnit(0);
                    hospitalBase.setInputUnitType(1);
                }else {
                    hospitalBase.setInputUnit(user.getUnitId());
                    hospitalBase.setInputUnitType(2);
                }
                hospitalBase.setInputTime(new Date());
            }

        }
        jyHospitalBaseService.insert(hospitalBase);
        Wrapper<JyDeclarationForm> wrapper = new EntityWrapper<>();
        wrapper.eq("apply_hospital_id", hospitalBase.getId());
        List<JyDeclarationForm> forms = jyDeclarationFormService.selectList(wrapper);
        JyDeclarationForm form = null;
        if (forms != null && forms.size()>0){
            form = forms.get(0);
        }else {
            form = new JyDeclarationForm();
        }
        if (hospitalBase.getHospitalType().intValue() == 4){
            //私立医院
            form.setApplyHospitalId(hospitalBase.getId());
            form.setApplyStatus(1);
            jyDeclarationFormService.insertOrUpdate(form);
        }

        return new SimpleCoreResponse("200","增加成功",null);
    }
    /**
     * 增加医院的基础信息
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("/updateHospitalBase")
    @AdscmLogger("修改医院的基础信息")
    public SimpleCoreResponse updateHospitalBase(@RequestBody JyHospitalBase hospitalBase, HttpServletRequest request) throws AdscmSystemException {

        if (hospitalBase.getHospitalType() == null ){
            throw new AdscmSystemException("请选择医院类型");
        }
        jyHospitalBaseService.updateById(hospitalBase);

        Wrapper<JyDeclarationForm> wrapper = new EntityWrapper<>();
        wrapper.eq("apply_hospital_id", hospitalBase.getId());
        List<JyDeclarationForm> forms = jyDeclarationFormService.selectList(wrapper);
        JyDeclarationForm form = null;
        if (forms != null && forms.size()>0){
            form = forms.get(0);
        }else {
            form = new JyDeclarationForm();
        }
        if (hospitalBase.getHospitalType().intValue() == 4){
            //私立医院
            form.setApplyHospitalId(hospitalBase.getId());
            form.setApplyStatus(1);
            jyDeclarationFormService.insertOrUpdate(form);
        }


        return new SimpleCoreResponse("200","修改成功",null);
    }
    /**
     * 获取医院的类别、级别
     * @param type ： 1 类别，2 级别
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("/getHospitalTypeLevel/{type}")
    @AdscmLogger("获取医院的类别、级别")
    public SimpleCoreResponse getHospitalTypeLevel(@PathVariable int type) throws AdscmSystemException {

        return new SimpleCoreResponse("200","查询成功",jyHospitalBaseService.queryHospitalTypeLevel(type));
    }
    /**
     * 根据字符串，模糊匹配医院名称
     * @param name
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("/queryLikeName")
    @AdscmLogger("根据字符串，模糊匹配医院名称")
    public SimpleCoreResponse queryLikeName(String name) throws AdscmSystemException {


        return new SimpleCoreResponse("200","查询成功",jyHospitalBaseService.queryHospitalListByLikeName(name));
    }

}

